import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {CheckstandPage} from "../checkstand/checkstand";
import {ROUTER} from "../../service/router.service";
import moment from "moment"
import {APARTMENTAPISERVICE} from "../../api/apartment-api.service";
import {GoodModel} from "../../model/good.model";
import {LocalStorage} from "../../utils/localstorage.utils";
import {GOODS} from "../../api/goods-api";
import {CheckStandModel} from "../../model/checkstand.model";

/**
 * Generated class for the CleanOrderPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-clean-order',
  templateUrl: 'clean-order.html',
})
export class CleanOrderPage {
  data = {
    startTime: moment().format(),
    layoutId:undefined
  };
  good:GoodModel;

  layoutList = [];

  moneyItems = [
    {
      name: '家电清洁',
      item: '100元/次'
    },
    {
      name: '合计',
      item: '100.00元'
    }
  ];

  constructor(public GOODS:GOODS, public ls:LocalStorage, public navCtrl:NavController, public navParams:NavParams, private ROUTER:ROUTER, private apartmentApi:APARTMENTAPISERVICE) {
  }

  loadMyHouse() {
    this.apartmentApi.getMyRentingHouse({}, (res:any)=> {
      this.layoutList = res;
      if(this.layoutList.length > 0){
        this.data.layoutId = this.layoutList[0].id
      }
    })
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad CleanOrderPage');
    this.good = this.ls.getObject('good') as GoodModel;
    this.moneyItems[0].name = this.good.title;
    this.moneyItems[0].item = this.moneyItems[1].item = this.good.salesPrice;
    this.loadMyHouse();
  }

  gotoCheckstand() {
    let reqData = {
      totalMoney:this.moneyItems[1].item,
      houseId: this.data.layoutId,
      goodsId: this.good.id,
      source: 1,
      serviceTime: moment(this.data.startTime).format("YYYY-MM-DD HH:mm:ss")
    }
    this.GOODS.purchaseClean(reqData, (v)=> {
      console.log(v);
      let checkstand = new CheckStandModel(v.orderNo, v.title, v.totalMoney, v.id,ROUTER.MACLEANDATAIL)
      this.ls.setObject('checkstand', checkstand);
      this.ROUTER.go(ROUTER.CHECK);
    })
  }

  ionViewDidEnter() {
    document.title = '品质保洁';
  }

}
